package io.github.talelin.latticy.controller.api;

import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.bo.PageCounterBO;
import io.github.talelin.latticy.common.CommonUtil;
import io.github.talelin.latticy.dto.DealRecordDTO;
import io.github.talelin.latticy.entity.Record;
import io.github.talelin.latticy.service.RecordService;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@RequestMapping("/api/record")
@Validated
public class RecordController {

    @Autowired
    private RecordService recordService;

    /**
     * 用户提交记录
     */
    @GetMapping("/user")
    @LoginRequired
    public PageResponseVO getUserRecord(@RequestParam(required = false, defaultValue = "0")
                                        @Min(value = 0) int start,
                                        @RequestParam(required = false, defaultValue = "20")
                                        @Min(value = 1) int count) {
        PageCounterBO pageCounter = CommonUtil.converToPageParamter(start, count);
        Page<Record> paging = recordService.getUserRecords(pageCounter.getPage(), pageCounter.getSize());
        return new PageResponseVO<>(paging.getTotalElements(), paging.getContent(), paging.getNumber(), paging.getSize());
    }

    /**
     * 审批列表
     */
    @GetMapping("/examine-user")
    @LoginRequired
    public PageResponseVO getExamineUserRecord(@RequestParam(required = false, defaultValue = "0")
                                               @Min(value = 0) int start,
                                               @RequestParam(required = false, defaultValue = "20")
                                               @Min(value = 1) int count,
                                               @RequestParam(name = "is_deal") Boolean isDeal) {
        PageCounterBO pageCounter = CommonUtil.converToPageParamter(start, count);
        Page<Record> paging = recordService.getExamineUserRecords(isDeal, pageCounter.getPage(), pageCounter.getSize());
        return new PageResponseVO<>(paging.getTotalElements(), paging.getContent(), paging.getNumber(), paging.getSize());
    }

    /**
     * 抄送我的
     */
    @GetMapping("/root-user")
    @LoginRequired
    public PageResponseVO getRootUserRecord(@RequestParam(required = false, defaultValue = "0")
                                            @Min(value = 0) int start,
                                            @RequestParam(required = false, defaultValue = "20")
                                            @Min(value = 1) int count) {
        PageCounterBO pageCounter = CommonUtil.converToPageParamter(start, count);
        Page<Record> paging = recordService.getRootUserRecords(pageCounter.getPage(), pageCounter.getSize());
        return new PageResponseVO<>(paging.getTotalElements(), paging.getContent(), paging.getNumber(), paging.getSize());
    }

    /**
     * 记录详情
     */
    @GetMapping("/{id}")
    @LoginRequired
    public Record get(@PathVariable @Positive Long id) {
        return recordService.getRecord(id);
    }

    /**
     * 审批记录
     */
    @PutMapping("/{id}")
    @LoginRequired
    public UpdatedVO deal(@PathVariable @Positive Long id,
                          @RequestBody @Validated DealRecordDTO dto) {
        recordService.updateRecordStatus(id, dto.getStatus());
        return new UpdatedVO<>();
    }


}
